if (!johnny) {
	var johnny = {}
};


$.extend(true, johnny, {
	registerLazyTab : function (key,fun){
		$("a[href='#"+key+"]").click(function (e) {
			   if (!$(this).hasClass("inited")){
				   $(this).addClass("inited");
				   setTimeout(fun,400);
			   }
		});
	},	
	getForSelect : function (value,selected){
	  var mainSpan = $("<span>");
	    $("<input>").addClass("span12 styled").attr({
	        class: 'forselect',
	        type: "checkbox",
	        value: value,
	        checked :  (selected)? true : false
	    }).appendTo(mainSpan);
	    return mainSpan.html();
	
	},
	getGridLink : function (conf){
		var mainSpan = $("<span>");
		var controlDiv = $("<div>").addClass("controls center").appendTo(mainSpan);
		$(conf).each(function(index,value){
			if (value.sub && value.sub.length >1){
 				johnny.getSubActionlink(value.css,value.sub,value.target).appendTo(controlDiv);
 				
			}else{
				johnny.getActionLink(value.link,value.text,value.css,value.dataId,value.target).appendTo(controlDiv);				
			}
 			
		});
		return mainSpan.html();
 	},
	getImageHtml : function(link){
			
		
 			var mainSpan = $("<span/>").append($("<div>").append($("<a>",{target : "_blank", href : link   }).append($("<img/>").attr({src:link}))));
  			return mainSpan.html();
 	},
 	getRedirectHtml : function(link){
			var mainSpan = $("<span/>").append($("<div>").append($("<a>",{target : "_blank", href : link   }).text("Click")));
  			return mainSpan.html();
 		 
 	},
 	
	getActionHtml : function (link, title,icon){
		var mainSpan = $("<span>");
		var controlDiv = $("<div>").addClass("controls center").appendTo(mainSpan);
		johnny.getActionLink(link,title,icon).appendTo(controlDiv);
		return mainSpan.html();
	},
	getEditHtml : function (link){
		return johnny.getActionHtml(link,"edit","icomoon-icon-pencil");
	}, 
	
	getInlineHtml : function (link){
		return johnny.getActionHtml(link,"in-line","icomoon-icon-eyedropper");
	},
	getActionLink : function (link , title,icon ,dataId,target){
		var href = $("<a>").attr({
			href: link,
			title: title,
			dataid : (dataId)? dataId:"#",
			target : (target)? target :"_self"
		}).addClass("tip");
		var span = $("<span>").addClass(icon+" icon12").appendTo(href);
		return href;
	},
	
	getSubActionlink : function(icon,sub,target){
		var href = $("<a>").attr({
			href: "#",
			"data-toggle" : "dropdown",
		}).addClass("dropdown-toggle");
		var span = $("<span>").addClass(icon+" icon12").appendTo(href);
		var ul = $("<ul>").addClass("dropdown-menu");
		
		
		$(sub).each(function(step,value){
			var li = $('<li>').appendTo(ul);
			var a = $('<a>',{
				href: value.url,
				text : value.name,
				target : (target)? target :"_self"
				}).appendTo(li);
			$('<span class="i"></span>').appendTo(li);
		});
	var result = 	$('<div class="btn-group dropup"/>').append(href).append(ul);
	console.dir(result.html());
	return result;
	},
	
	getInlineAndEdit : function (edit, inline,view){

		var editHtml = johnny.getActionLink(edit,"edit","icomoon-icon-pencil");

		var mainSpan = $("<span>");
		var controlDiv = $("<div>").addClass("controls center").appendTo(mainSpan);
		if ( view != undefined){
			johnny.getActionLink(view,"view","minia-icon-eye").appendTo(controlDiv);
		}

		editHtml.appendTo(controlDiv);
		if (inline!= null){
			var inlineHtml = johnny.getActionLink(inline,"in line edit","icomoon-icon-eyedropper");
			inlineHtml.appendTo(controlDiv);
		}
		return mainSpan.html();
		
		
	},
	
	message : function(type, text) {
				if('warning' == type){
					johnny.warning(text);
				}else if ('error' == type){
					
					johnny.error(text);
				}else if ('info' == type){
					johnny.info(text);
					
				}else if ('success' == type){
					
					johnny.success(text);
				}
	},
	info : function (text){
		var icon = 'brocco-icon-info';
		var type = 'info';
		johnny.showNotify(icon,type,text);
	},
	success : function (text){
		var icon = 'picon icon16 iconic-icon-check-alt white';
		var type = 'success';
		johnny.showNotify(icon,type,text);
	},
	error : function (text){
		var icon = 'entypo-icon-warning';
		var type = 'error';
		johnny.showNotify(icon,type,text);
	},
	warning : function (text){
		var icon = 'entypo-icon-warning';
		var type = 'error';
		johnny.showNotify(icon,type,text);
	},
	showNotify: function (icon, type, text){
		$.pnotify({
		    title: 'Notice',
			type:  type,
		    text:  text ,
		    icon: 'picon icon16 ' +icon +' white',
		    opacity: 0.95,
		    sticker: false,
		    history: false
		});
	},
	initColumnHeader : {},
	initTableQuery : function (name,container){
		if (name){
		var searchLabel = $("<label>").html("Search:").css("display", "inline");
		var filterDiv = $("<div>").addClass("dataTables_filter").append(searchLabel).append(name);
		
		if (container){
			var x = $(container).find('.for_filter');
			if (x.length>0){
				x.append(filterDiv);
	 		}else{
			$('.dataTables_wrapper').prepend(filterDiv);
			}	
			
		}else{
			var x = $('.for_filter');
			if (x.length>0){
				x.append(filterDiv);
	 		}else{
			$('.dataTables_wrapper').prepend(filterDiv);
			}
			}	
		
		}
		
		
		
		
		
		johnny.resetFilter($('.reset'));
 		$(".datepickerlazy").datepicker({
			dateFormat: 'yy-mm-dd',
			onSelect: function () {
				johnny.updateGrid();
			}
		}).keydown(function (event) {
			if (event.keyCode == 8) { $(".datepickerlazy").val(""); }
			else { event.preventDefault(); }
		});
		$(".filterdata").keyup(function () {
			johnny.updateGrid();
		});
		
		$(".filterdata").change(function () {
			johnny.updateGrid();
		});
	},
	oTime : {},
	oTable :{},
	updateGrid : function (){
		window.clearTimeout(johnny.oTime);
		johnny.oTime = setTimeout(function () { johnny.oTable.fnDraw(); }, 500);
	},
	tableBtn : function (config){
		var span
 		if (config.selector){
		span = $(config.selector);	
		
		}else{
		span = 	$('h4 > span.right');
		}
		if (span.length){
			
	
			
			
		}else{
		 
 		 var bar = $('h4');
		 if (bar.length>1){
			 
 			 var button = $('h4.button-bar');
 			 if (button.length ==1){
				 bar = button;
 			 }
		 }else{
			 
			 
		 }
		 span = 	$('<span/>', {
			    "class" : 'box-form right',
	 		}).appendTo(bar);
	  	}
	  	
	  	$('<a/>',{
			href : '#',
			id : config.id,
			class : 'btn btn-mini',
	   	  }).text(config.text).appendTo(span);
 		
		$('#'+config.id).click(function(){
			
			var items = johnny.select(config.selects);
			if(items && items.length <=0){
				johnny.warning("Please select at least one " +config.domain + " for "+config.operation);
				return false;
			}
			if (config.max){
				if (items.length > config.max){
					johnny.warning("Only " + config.max + " items allowed for "+config.operation);
					return false;
					
					
				}
				
			}
			
			
 			if(confirm("Confirm to "+config.operation +" the selected " + config.domain+" ?")){
				$.ajax({
					url : config.url,
					type : "POST",
					data : {"_method" : "PUT", "ids" : items},
					dataType : "json",
					success : function(data){
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
							johnny.error(data.message);
							return;
						}
						johnny.success(config.operation + " " + config.domain+ " successfully");
						if (config.selects){
							config.selects.dataTable().fnDraw();		
						}else{
						johnny.updateGrid();
						}
					}
				});
				
				return false;
			}
			
			return false;
		});
		
		
	}
});

$(function () {
	var selected = "icomoon-icon-checkmark-2";
	var blocked = "icomoon-icon-blocked";
	setTimeout(function () { 
		$('.dataTables_length select').uniform();	
 	}, 200);
	
	if (("#select_all , .select_all").length != 0){
		
 		$("#select_all , .select_all").click(function(){
 			var click = $(this);	
 			var table = click.parents("table:first");
  			var check = (table.find('.'+selected).length == 0);	
  			table.find('.forselect').each(function(index){
	 
				$(this).attr('checked', !check);
				 $.uniform.update($(this));
  			});
		if (!check){
			table.find('.'+selected).each(function(index){
				$(this).addClass("icomoon-icon-blocked");
				$(this).removeClass("icomoon-icon-checkmark-2");
 		});
		}else{
			table.find('.icomoon-icon-blocked').each(function(index){
				$(this).addClass("icomoon-icon-checkmark-2");
				$(this).removeClass("icomoon-icon-blocked'");
 
		});
 		}
  	 });	


	}

	
    $.ajaxSetup({
        error: function (jqXHR, exception) {
            if (jqXHR.status === 0) {
                johnny.error('Not connect. Please verify network.');
            } else if (jqXHR.status == 401) {
                johnny.error('No permission to view that page. \nMaybe because of your session timeout.');
            } else if (jqXHR.status == 404) {
                johnny.error('Requested page not found.');
            } else if (jqXHR.status == 500) {
                johnny.error('Internal Server Error. \nPlease inform IT Support if it happends again.');
            } else if (exception === 'parsererror') {
                johnny.error('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                johnny.error('Time out error.');
            } else if (exception === 'abort') {
                johnny.error('Ajax request aborted.');
            } else {
                johnny.error('Uncaught Error.\n' + jqXHR.responseText);
            }
        },
        statusCode: {
            500: function (xhr) {
                johnny.warning("Internal Server Error. \nPlease inform IT Support if it happends again.");
            },
            401: function (xhr) {
                johnny.warning("No permission to view that page. \nMaybe because of your session timeout.");
            }
        }
    });

    
});